package com.ogawa.a7517.adapter;

import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.ProgramBean;

import java.util.List;

public class AutoAdapter extends BaseQuickAdapter<ProgramBean, BaseViewHolder> {
    private boolean auto;
    private boolean showDelete;
    private Resources resources;
    private int running;

    public AutoAdapter(List<ProgramBean> data, boolean auto) {
        super(0, data);
        this.auto = auto;
    }

    public void setShowDelete(boolean showDelete) {
        this.showDelete = showDelete;
        notifyDataSetChanged();
    }

    public void setRunning(int running) {
        if (this.running!=running){
            this.running = running;
            notifyDataSetChanged();
        }
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_program_item, parent, false);
        return createBaseViewHolder(inflate);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ProgramBean item) {
        helper.setImageResource(R.id.iv_program, item.getImgRes());
        helper.setText(R.id.tv_program, item.getName());
        if (resources == null) {
            resources = helper.itemView.getContext().getResources();
        }
        helper.setTextColor(R.id.tv_program, auto ? resources.getColor(R.color.white)
                : resources.getColor(R.color.color6B9B));
        helper.setVisible(R.id.iv_delete, showDelete);
        helper.getView(R.id.iv_program).setSelected(running == item.getModel());
        helper.itemView.setSelected(running == item.getModel());
    }
}
